home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / UI / UI_PBM.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  7KB  |  347 lines

  1. /***************************************************************
  2.  
  3.     ui_pbm.c        Portable Bit-Map Routines
  4.             for the Bywater Graphical User Interface
  5.  
  6.             Copyright (c) 1991, Ted A. Campbell
  7.  
  8.             Bywater Software
  9.             P. O. Box 4023 
  10.             Duke Station 
  11.             Durham, NC  27706
  12.  
  13.             email: tcamp@hercules.acpub.duke.edu
  14.  
  15.     Copyright and Permissions Information:
  16.  
  17.     All U.S. and international copyrights are claimed by the
  18.     author. The author grants permission to use this code
  19.     and software based on it under the following conditions:
  20.     (a) in general, the code and software based upon it may be 
  21.     used by individuals and by non-profit organizations; (b) it
  22.     may also be utilized by governmental agencies in any country,
  23.     with the exception of military agencies; (c) the code and/or
  24.     software based upon it may not be sold for a profit without
  25.     an explicit and specific permission from the author, except
  26.     that a minimal fee may be charged for media on which it is
  27.     copied, and for copying and handling; (d) the code must be 
  28.     distributed in the form in which it has been released by the
  29.     author; and (e) the code and software based upon it may not 
  30.     be used for illegal activities. 
  31.  
  32. ***************************************************************/
  33.  
  34.  
  35. #include "stdio.h"
  36. #include "ctype.h"
  37.  
  38. #ifdef  __STDC__
  39. #include "malloc.h"
  40. #include "stdlib.h"
  41. #else
  42. extern char * malloc();
  43. #include "math.h"
  44. #endif
  45.  
  46. #include "bw.h"
  47. #include "gr.h"
  48. #include "ui.h"
  49.  
  50. #define BUFSIZE 128
  51. #define TESTINT 0
  52. #define ACC 50
  53.  
  54. extern char *pbm_fgets();
  55.  
  56. ui_pbmread( file, pmstruct, foreground, background )
  57.    char *file;
  58.    struct pbm_struct *pmstruct;
  59.    int foreground, background;
  60.    {
  61.    FILE *fp;
  62.    char *p;
  63.    static int w, h, i, ctr;
  64.    register int x, y;
  65.    static char tbuf[ BW_EBUFSIZE ];
  66.  
  67.    if ( ( fp = fopen( file, "r" ) ) == NULL )
  68.       {
  69.       strcpy( tbuf, ui_fontpath );
  70.       strcat( tbuf, file );
  71.       if ( ( fp = fopen( tbuf, "r" ) ) == NULL )
  72.      {
  73.      strcpy( tbuf, "../../fonts/" );
  74.      strcat( tbuf, file );
  75.      if ( ( fp = fopen( tbuf, "r" ) ) == NULL )
  76.         {
  77. #ifdef OLD_DEBUG
  78.         fprintf( stderr, "Failed to open file %s \n", file );
  79. #endif
  80.         return FALSE;
  81.         }
  82.      }
  83.       }
  84.  
  85. #ifdef OLD_DEBUG
  86.    sprintf( bw_ebuf, "Opened: %s   ", file );
  87.    bw_debug( bw_ebuf );
  88. #endif
  89.  
  90.    /* Read first line to check for PBM magic number */
  91.  
  92.    p = pbm_fgets( tbuf, BUFSIZE, fp );
  93.    if ( tbuf[ 0 ] != 'P' )
  94.       {
  95. #ifdef DEBUG
  96.       sprintf( bw_ebuf, "File %s is not in PBM format", file );
  97.       bw_error( bw_ebuf );
  98. #endif
  99.       return BW_ERROR;
  100.       }
  101.    if ( tbuf[ 1 ] != '1' )
  102.       {
  103. #ifdef DEBUG
  104.       sprintf( bw_ebuf, "File %s is not in PBM format", file );
  105.       bw_error( bw_ebuf );
  106. #endif
  107.       return BW_ERROR;
  108.       }
  109.  
  110.    /* Read next line to get width and height */
  111.  
  112.    p = pbm_fgets( tbuf, BUFSIZE, fp );
  113.    sscanf( tbuf, "%d %d", &w, &h );
  114.    pmstruct->xsize = w;
  115.    pmstruct->ysize = h;
  116.  
  117. #ifdef OLD_DEBUG
  118.    sprintf( bw_ebuf, "Sizes read: x: %03d  y: %03d   ", pmstruct->xsize, pmstruct->ysize );
  119.    bw_debug( bw_ebuf );
  120. #endif
  121.  
  122.    /* display the bitmap in hidden memory before getting image */
  123.  
  124.    y = 0;
  125.    while ( ( y < pmstruct->ysize ) && ( feof( fp ) == 0 ))
  126.       {
  127.       x = 0;
  128.       p = pbm_fgets( tbuf, BUFSIZE, fp );
  129.       ctr = 0;
  130.       if ( p == NULL )
  131.      {
  132. #ifdef DEBUG
  133.      bw_error( "PBM file terminated prematurely" );
  134. #endif
  135.      return FALSE;
  136.      }
  137.       while( ( x < pmstruct->xsize ) && ( tbuf[ ctr ] != '\n' ))
  138.      {
  139.      i = pbm_nextc( tbuf, &ctr );
  140.      if ( i == 0 )
  141.         {
  142.         gr_pixel( GR_HIDDEN, x, ( pmstruct->ysize - y), background );
  143.         }
  144.      else if ( i == 1 )
  145.         {
  146.         gr_pixel( GR_HIDDEN, x, ( pmstruct->ysize - y), foreground );
  147.         }
  148.      ++x;
  149.      }
  150.       ++y;
  151.       }
  152.  
  153.    /* close the bitmap file */
  154.  
  155.    fclose( fp );
  156.  
  157.    /* save the image */
  158.  
  159.    gr_imsave( GR_HIDDEN, TRUE, 0, 1, x - 1, y, &( pmstruct->image ) );
  160.  
  161.    return TRUE;
  162.  
  163.    }
  164.  
  165. ui_pbmshow( screen, x, y, pmstruct )
  166.    int screen;
  167.    int x, y;
  168.    struct pbm_struct *pmstruct;
  169.    {
  170.  
  171.    if ( pmstruct == NULL )
  172.       {
  173. #ifdef DEBUG
  174.       bw_error( "ui_pmshow() received NULL pbm structure" );
  175. #endif
  176.       return BW_ERROR;
  177.       }
  178.  
  179. #ifdef OLD_DEBUG
  180.    bw_debug( "Ready to show image" );
  181. #endif
  182.    gr_imsave( screen, FALSE, x, y + 1, x + pmstruct->xsize - 1,
  183.       y + pmstruct->ysize - 1, &( pmstruct->image ) );
  184.  
  185.    }
  186.  
  187. char *
  188. pbm_fgets( string, n, stream )
  189.    char *string;
  190.    int n;
  191.    FILE *stream;
  192.    {
  193.    register int c;
  194.    char *p;
  195.  
  196.    c = TRUE;
  197.    while( c == TRUE )
  198.       {
  199.       p = fgets( string, n, stream );
  200.       if ( p == NULL )
  201.      {
  202.      return NULL;
  203.      }
  204.       if ( string[ 0 ] != '#' )
  205.      {
  206. #ifdef OLD_DEBUG
  207.      sprintf( bw_ebuf, "read line <%s>", string );
  208.      bw_debug( bw_ebuf );
  209. #endif
  210.      return p;
  211.      }
  212.       }
  213.    }
  214.  
  215. pbm_nextc( s, ctr )
  216.    char *s;
  217.    int *ctr;
  218.    {
  219.    static char tbuf[ 12 ];
  220.    register int c;
  221.  
  222. #ifdef OLD_DEBUG
  223.    bw_message( "entered pbm_nextc()" );
  224. #endif
  225.  
  226.    while( isspace( s[ *ctr ] ) != FALSE )
  227.       {
  228.       ++*ctr;
  229.       }
  230.  
  231.    c = 0;
  232.    while( isspace( s[*ctr ] ) == FALSE )
  233.       {
  234.       tbuf[ c ] = s[ *ctr ];
  235.       ++*ctr;
  236.       ++c;
  237.       tbuf[ c ] = 0;
  238.       }
  239.  
  240. #ifdef OLD_DEBUG
  241.    sprintf( bw_ebuf, "read char %d", atoi( tbuf ) );
  242.    bw_debug( bw_ebuf );
  243. #endif
  244.  
  245.    return atoi( tbuf );
  246.    }
  247.  
  248. ui_pbmcenter( screen, x1, y1, x2, y2, pmstruct )
  249.    int screen;
  250.    int x1, y1, x2, y2;
  251.    struct pbm_struct *pmstruct;
  252.    {
  253.    register int x, y;
  254.  
  255.    /* calculate coordinates */
  256.  
  257.    x = x1 + ((( x2 - x1 ) - ( pmstruct->xsize )) / 2 );
  258.    y = y1 + ((( y2 - y1 ) - ( pmstruct->ysize )) / 2 );
  259.  
  260.    /* show the icon */
  261.  
  262.    ui_pbmshow( screen, x, y, pmstruct );
  263.  
  264.    }
  265.  
  266. ui_pbmtile( screen, x1, y1, x2, y2, pmstruct )
  267.    int screen;
  268.    int x1, y1, x2, y2;
  269.    struct pbm_struct *pmstruct;
  270.    {
  271.    register int x, y;
  272.    static int image;
  273.  
  274.    /* check size */
  275.  
  276.    if ( ( x2 - x1 ) < pmstruct->xsize )
  277.       {
  278. #ifdef DEBUG
  279.       sprintf( bw_ebuf, "Area too small: x1 %d x2 %d:  xsize %d",
  280.      x1, x2, pmstruct->xsize );
  281.       bw_error( bw_ebuf );
  282. #endif
  283.       return BW_ERROR;
  284.       }
  285.  
  286.    if ( ( y2 - y1 ) < pmstruct->ysize )
  287.       {
  288. #ifdef DEBUG
  289.       sprintf( bw_ebuf, "Area too small: y1 %d y2 %d:  ysize %d",
  290.      y1, y2, pmstruct->ysize );
  291.       bw_error( bw_ebuf );
  292. #endif
  293.       return BW_ERROR;
  294.       }
  295.  
  296. #ifdef OLD_DEBUG
  297.    sprintf( bw_ebuf, "x1 %d, y1 %d, x2, %d, y2 %d, xsize, %d, ysize %d",
  298.       x1, y1, x2, y2, pmstruct->xsize, pmstruct->ysize );
  299.    bw_debug( bw_ebuf );
  300.    bw_message( "           " );
  301. #endif
  302.  
  303.    /* fill area with tile except right and top areas */
  304.  
  305.    for ( x = x1; x < ( x2 - pmstruct->xsize ); x += pmstruct->xsize )
  306.       {
  307.  
  308. #ifdef OLD_DEBUG
  309.       sprintf( bw_ebuf, "x = %d", x );
  310.       bw_debug( bw_ebuf );
  311.       bw_message( "           " );
  312. #endif
  313.       for ( y = y1; y < ( y2 - pmstruct->ysize ); y += pmstruct->ysize )
  314.      {
  315. #ifdef OLD_DEBUG
  316.      sprintf( bw_ebuf, "y = %d", y );
  317.      bw_debug( bw_ebuf );
  318.      bw_message( "               " );
  319. #endif
  320.      ui_pbmshow( screen, x, y, pmstruct );
  321. #ifdef OLD_DEBUG
  322.      bw_debug( "icon displayed" );
  323.      bw_message( "              " );
  324. #endif
  325.      }
  326.  
  327.       }
  328.  
  329.    /* first the right remaining area */
  330.  
  331.    if ( x != x2 )
  332.       {
  333.       gr_imsave( screen, TRUE, x1, y1, x1 + ( x2 - x ), y, &image );
  334.       gr_imsave( screen, FALSE, x, y1, x2, y, &image );
  335.       }
  336.    gr_imfree( image );
  337.  
  338.    /* then the top remaining area */
  339.  
  340.    if ( y != y2 )
  341.       {
  342.       gr_imsave( screen, TRUE, x1, y1, x2, y1 + ( y2 - y ), &image );
  343.       gr_imsave( screen, FALSE, x1, y, x2, y2, &image );
  344.       }
  345.    gr_imfree( image );
  346.    }
  347.